package org.lanqiao.sjx.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.Item;
import org.lanqiao.bean.Zuopin;
import org.lanqiao.sjx.dao.ItemDao;
import org.lanqiao.sjx.dao.impl.ItemImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/Itemservlet")
public class Itemservlet extends BaseServlet {
	ItemDao dao=new ItemImpl();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request=request;
		this.response=response;
		
		String type = getString("type");
		redata.setMsg("");
		if (!isNull("type")) {
			switch (type) {
			case "select":
				selectItem();
				break;
			case "insert":
				insertItem();
				break;
			case "update":
				updateItem();
				break;
			case "delete":
				deleteItem();
				break;
				default:
					break;
			}		
		}
		else {
			redata.setCode(3);
			redata.setData(null);

		}
		System.out.println(redata);
		response.getWriter().write(new Gson().toJson(redata));
	}

	private void deleteItem() {
		// TODO Auto-generated method stub
		
	}
	
	
	private void updateItem() {
		System.out.println("updateitem");
		Integer iid = getInt("id");
		String itemName = getString("itemName");
		String itemDescription = getString("itemDescription");
		String job = getString("job");
		Integer startYear = getInt("startYear");
		Integer startMonth = getInt("startMonth");
		Integer endYear= getInt("endYear");
		Integer endMonth= getInt("endMonth");
		
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setMsg("");
			return;
		}
		
		Integer i=dao.updateItem(new Item(itemName, job, startYear, startMonth, endYear, endMonth, itemDescription, iid));
		if (i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("修改成功");
		}	
	}
	
	
	
	private void insertItem() {
		System.out.println("insertitem");
		Integer userId = getInt("userId");
		String itemName = getString("itemName");
		String itemDescription = getString("itemDescription");
		String job = getString("job");
		Integer startYear = getInt("startYear");
		Integer startMonth = getInt("startMonth");
		Integer endYear= getInt("endYear");
		Integer endMonth= getInt("endMonth");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			return;
		}
		Integer i=dao.insertItem(new Item(userId, itemName, job, startYear, startMonth, endYear, endMonth, itemDescription));
		if (i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("添加成功");
		}
		
	}
	private void selectItem() {
		System.out.println("selectitem");
		Integer userId = getInt("userId");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setMsg("");
			return;
		}
		List<Item> selectItem = dao.selectItem(userId);
			redata.setCode(2);
			redata.setData(selectItem);
			redata.setMsg("查询成功");		
	}
	
}


